package com.glsc.ngateway.platform.service.dev;

import com.glsc.ngateway.common.api.common.dto.PageDataResult;
import com.glsc.ngateway.common.api.flowable.dto.demand.FlowableAssigneeDto;
import com.glsc.ngateway.common.api.flowable.dto.demand.FlowableDemandDto;
import com.glsc.ngateway.common.api.flowable.dto.demand.node.ProcessNode;
import com.glsc.ngateway.common.api.flowable.dto.param.ParamCommentDto;
import com.glsc.ngateway.common.api.flowable.feign.IFeignFlowableService;
import com.glsc.ngateway.common.api.flowable.resp.FlowableResponse;
import com.glsc.ngateway.common.api.flowable.resp.ResponseCommentDto;
import com.glsc.ngateway.common.api.flowable.resp.ResponseSituationDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DemandService {

    Logger logger = LoggerFactory.getLogger(DemandService.class);

    @Autowired
    IFeignFlowableService feignDemandService;


    /**
     * 根据processId获取一个需求流程的相关信息
     *
     * @param processId
     * @return
     */
    public ResponseSituationDto getProcessInfo(String processId) {

        FlowableResponse<ResponseSituationDto> processinfo = feignDemandService.getProcessInfo(processId);
        if (processinfo.getStatus() != 0) {
            return null;
        }
        return processinfo.getData();
    }


    /**
     * 根据processId获取一个需求流程的相关要素信息
     *
     * @param processId
     * @return
     */
    public FlowableDemandDto getRmptInfo(String processId) {

        FlowableResponse<FlowableDemandDto> demandinfo = feignDemandService.getDemandInfo(processId);
        if (demandinfo.getStatus() != 0) {
            return null;
        }
        return demandinfo.getData();

    }


    /**
     * 根据processId获取一个需求流程的当前节点TaskId和Title和SysId
     *
     * @param processId
     * @return
     */
    public String[] getCurrentTaskIdAndTitleAndSysid(String processId) {
        ResponseSituationDto processInfo = getProcessInfo(processId);
        if (processInfo == null) return null;
        String[] res = new String[3];
        res[0] = processInfo.getCurrentTaskId();
        res[1] = processInfo.getProcessTitle();
        res[2] = processInfo.getSysid();
        return res;

    }


    /**
     * 根据processId获取某个节点上的所有comments信息
     *
     * @param processId
     * @param processNode
     * @return
     */
    public List<ResponseCommentDto> getComments(String processId, ProcessNode processNode) {
        List<ResponseCommentDto> ret = new ArrayList<ResponseCommentDto>();
        ParamCommentDto param = new ParamCommentDto();
        param.setProcessId(processId);
        /*一般一个流程应该不会有超过100条commnet吧*/
        FlowableResponse<PageDataResult<ResponseCommentDto>> nwret = feignDemandService.queryCommentsByProcessId(1, 100, param);
        if (nwret.getStatus() == 0) {
            nwret.getData().getList().forEach(r -> {
                if (processNode.toString().equalsIgnoreCase(r.getTaskDefinitionKey())) {
                    ret.add(r);
                }
            });
        }
        return ret;
    }

    /**
     * 判断当前用户是否是某个系统的管理员
     *
     * @param userAccount
     * @param sysid       系统表示
     * @return
     */
    public boolean beSystemManager(String userAccount, String sysid) {
        try {
            FlowableResponse<List<FlowableAssigneeDto>> response = feignDemandService.findAssigneeByProcessDefinitionKey("DemandManagement", sysid);
            List<FlowableAssigneeDto> data = response.getData();
            if ((data.stream().anyMatch(e -> e.getAssigneeVariable().equals("manager") && e.getAssigneeName().equals(userAccount)))
                    || (data.stream().anyMatch(e -> e.getAssigneeVariable().equals("leader") && e.getAssigneeName().equals(userAccount)))) {

                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            logger.error("beSystemManager error", e);
            return false;
        }
    }
}
